package com.zhiqian.community.config;

import com.zhiqian.community.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {

    //FactoryBean：工厂bean，可简化Bean的实例化过程
    /*
        1.通过FactoryBean封装Bean的实例化过程
        2.将FactoryBean装配到Spring容器中
        3.将FactoryBean注入到其他的Bean
        4.其他的Bean得到的是FactoryBean所管理的对象实例
     */
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail(){
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefresh");
        factoryBean.setGroup("communityJobGroup");
        factoryBean.setDurability(true);//是否长期保存
        factoryBean.setRequestsRecovery(true);//是否可恢复
        return factoryBean;
    }

    //SimpleTriggerFactoryBean 简单触发器配置
    //CronTriggerFactoryBean 复杂的触发器配置
    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail){
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        factoryBean.setRepeatInterval(1000*3600);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }
}
